const FundModel = require("../../model/fundmodel")
const FundRequest = require("../../request/fund")
const {
	isString,
	isArray,
	now,
} = require("../../utils")
module.exports = class FundLogic {

	static async addBaseInfoForFund() {
		const pageNum = 1000;
		const curDate = now("YYYY-MM-DD");
		const total = await this.getTotalNum();
		console.log("total",total);
		if(!total){
			return 200001
		}
		const fund = new FundModel();
		const count = Math.ceil(total/pageNum); 
		for(let i = 1 ; i <= count ; i ++){
			console.log(count,">>>",i);
			const dataList = await this.fetchBaseInfoForFund("data",i,pageNum);
			if(dataList && dataList.length){
			  const preDataList =	 dataList.map(mi => {
				  const fundInstance =	fund.getNeatInstance();
				  fundInstance.symbol = mi.symbol; //基金标识
				  fundInstance.name = mi.name; //基金名称
				  fundInstance.type_group = mi.Type2Id; //基金组类型
				  fundInstance.type = mi.Type3Id; //基金类型
				  fundInstance.company_name = mi.CompanyName; //基金公司
				  fundInstance.subject_name = mi.SubjectName; //主题概念
				  fundInstance.found_date = mi.clrq; //创立日期
				  fundInstance.latest_net_value =mi.dwjz; //最新净值
				  fundInstance.accumulate_net_value =mi.ljjz; //累积净值
				  fundInstance.scale =mi.jjgm; //基金规模(亿)
				  fundInstance.Year_todate_performance =mi.w1navg; //今年以来业绩
				  fundInstance.one_month_performance =mi.w4navg; //近一个月业绩
				  fundInstance.three_month_performance =mi.w13navg; //近三个月业绩
				  fundInstance.six_month_performance =mi.w26navg; //近六个月业绩
				  fundInstance.one_year_performance =mi.w52navg; //近一年业绩
				  fundInstance.three_year_performance =mi.y3navg; //近三年业绩
				  fundInstance.five_year_performance =mi.y5navg; //近五年业绩
				  fundInstance.cur_year_performance =mi.ynyl; //今年业绩
				  fundInstance.morningstar_rating =mi.cxpj; //晨星评级
				  fundInstance.haitong_rating =mi.htpj; //海通评级
				  fundInstance.jianjinxin_rating =mi.jajxpj; //济安金信评级
				  fundInstance.cmsc_Rating =mi.zspj; //招商评级
				  fundInstance.milky_way_rating =mi.yhpj5; //银河评级
				  fundInstance.manage_date = mi.CYTime; //基金管理日期(天)
				  fundInstance.exchange = mi.EXCHANGE; //交易所
				  fundInstance.manager_name = mi.managerName; //基金管理人
				  fundInstance.create_date = curDate; //创建日期
				  fundInstance.lastupdate_date = curDate; //更新日期
				  return fundInstance
				})
				 await fund.insert(preDataList);
			}
		}
		return 0;
	}
    static async getPageData(){
		 
	}
	static async getTotalNum() {
		return Number.parseInt(await this.fetchBaseInfoForFund("totalnum")) || 0
	}
	
    static async fetchBaseInfoForFund(prop = "data",page , num , params = {}){
		const {
			data: {
				result: {
					data: {
						[prop]:res
					} = {}
				} = {}
			} = {}
		} = await FundRequest.pageBaseInfoForFund(page,num,params) || {};
	    return res 
	}
}
